
package com.probiz.estore.system.service.impl;

import java.util.Iterator;
import java.util.List;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.model.system.MailQueue;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.system.dao.MailQueueDao;
import com.probiz.estore.system.service.MailQueueManager;

/**
 * Manager implementation for MailQueue, responsible for business processing,
 * and communicate between web and persistence layer.
 */
public class MailQueueManagerImpl extends GenericManagerImpl<MailQueue>
		implements MailQueueManager {
	

	public MailQueueDao	mailQueueDao	= null;


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = mailQueueDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(MailQueue entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(MailQueue entity) {

	}

	/**
	 * @param mailQueueDao
	 *            the mailQueueDao to set
	 */
	public void setMailQueueDao(MailQueueDao mailQueueDao) {
		this.mailQueueDao = mailQueueDao;
	}
	
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.MailQueueManager#fetchMailListToProcess(int)
	 */
	public List<MailQueue> fetchMailListToProcess(int numOfEmailsPerTime) {
		return mailQueueDao.fetchEmailListToProcess(numOfEmailsPerTime);
	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.MailQueueManager#cleanExpiredEmails()
	 */
	public void cleanExpiredEmails() {
		int daysToKeep = ConfigUtil.getInstance().getDaysToKeep();
		logger.debug("Cleaning expried emails which is older than x days: "
				+ daysToKeep);
		mailQueueDao.deleteExpiredEmails(daysToKeep);

	}

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.system.service.MailQueueManager#resetIncorrectEmails()
	 */
	public void resetIncorrectEmails() {
		if (logger.isDebugEnabled())
			logger
					.debug("Reseting emails with not normal status (e.g. sending).");

		mailQueueDao.resetIncorrectEmails();
	}
	
}
